﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;

public partial class Empolyee_RoleManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    private void DataBind()
    {
        int roleID;
        if (DropDownList1.SelectedIndex > -1)
        {
            roleID = Convert.ToInt32(DropDownList1.SelectedValue);
        }
        else
        {
            roleID = 0;
        }

        var deliverType = Convert.ToInt32(DropDownList2.SelectedValue);

        var delegates = new DelegateRoleBLL().GetDelegatesByRoleID(roleID, deliverType);


        foreach (RepeaterItem item in Repeater1.Items)
        {
            var checkbox = (CheckBox)item.FindControl("CheckBox1");

            if (checkbox != null)
            {
                checkbox.Checked = false;

                var delegateType = Convert.ToInt32(checkbox.ToolTip);
                if (delegates.Contains(delegateType))
                {
                    checkbox.Checked = true;
                }
            }
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        var delegates = new List<int>();

        foreach (RepeaterItem item in Repeater1.Items)
        {
            var checkbox = (CheckBox)item.FindControl("CheckBox1");

            if (checkbox != null)
            {
                if (checkbox.Checked)
                {
                    delegates.Add(Convert.ToInt32(checkbox.ToolTip));
                }
            }
        }

        var roleID = Convert.ToInt32(DropDownList1.SelectedValue);
        var deliverType = Convert.ToInt32(DropDownList2.SelectedValue);

        new DelegateRoleBLL().DefineDelegateRole(roleID, deliverType, delegates);
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBind();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBind();
    }
}